<%@ Page language="vb" CodeFile="settingsModel.aspx.vb" Inherits="settingsModel_aspx_vb" %>

<%
    rowNumber = 0
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        brandID = Request.QueryString("brandID")
        modelID = Request.QueryString("modelID")
        brandID = CLng(brandID)
%>
<script type="text/javascript">
  function confirmSubmit() { 
	if (window.confirm('Are you sure you want to delete this car model?')) {
		location.href='settingsModel.aspx?do=delete&brandID=<%= brandID %>&modelID=<%= modelID %>';
		return false;
	}
	else {
		return false;
	}
  }
  </script>
<%
        dbOpen()
        RecSet = Conn.Execute("Select brand From tblBrand Where brandID = " & brandID & "")
        If RecSet.EOF Then 
            Response.Redirect("settingsBrand.aspx")
        Else
            brand = RecSet.Fields("brand").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If Len(modelID) > 0 Then 
            modelID = CLng(modelID)
            dbOpen()
            RecSet = Conn.Execute("Select model, (Select Count(modelID) From tblVehicle Where modelID = " & modelID & ") As vehicleCount From tblModel Where modelID = " & modelID & "")
            If RecSet.EOF Then 
                Response.Redirect("settingsBrand.aspx")
            Else
                dbModel = RecSet.Fields("model").Value
                dbVehicleCount = RecSet.Fields("vehicleCount").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""model"", ""==''""]"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>CAR MODEL SETTINGS FOR " & UCase(brand) & "<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='settingsModel.aspx?do=save&brandID=" & brandID & "&modelID=" & modelID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Model:</td><td align='right'><input type='text' name='model' id='model' size='40' maxlength='50' value='" & dbModel & "'></td><td><div id='error_model' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td align='right' colspan='2'>")
        If CInt(dbVehicleCount) = 0 And CDbl(fragusLeasingUserTypeID) = 1 And Len(modelID) > 0 Then 
            Response.Write("<input type='button' value='Delete car model' onClick='return confirmSubmit()'>&nbsp; ")
        End IF
        Response.Write("<input type='submit' value='Save'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='5'>CURRENT CAR MODEL LIST FOR " & UCase(brand) & "<hr class='hrHeadline'></td></tr>")
        dbOpen()
        RecSet = Conn.Execute("Select m.modelID, m.model From tblModel m Where m.brandID = " & brandID & " Order by m.model")
        If RecSet.EOF Then 
            Response.Write("<tr><td><font color='red'>No car models found.</font></td></tr>")
        Else
            Do Until RecSet.EOF
                modelID = RecSet.Fields("modelID").Value
                model = RecSet.Fields("model").Value
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td><a href='settingsModel.aspx?brandID=" & brandID & "&modelID=" & modelID & "'>" & model & "</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr><td colspan='5'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        model = Request.Form("model")
        brandID = Request.QueryString("brandID")
        modelID = Request.QueryString("modelID")
        brandID = CLng(brandID)
        If Len(model) > 0 Then 
            model = Trim(model)
            model = Replace(CStr(model), "'", "")
            model = Replace(CStr(model), """", "")
            model = Replace(CStr(model), "<", "")
            model = Replace(CStr(model), ">", "")
            model = Replace(CStr(model), "=", "")
        End IF
        If Len(modelID) > 0 Then 
            modelID = CLng(modelID)
            dbOpen()
            Response.Write("Update tblModel Set model = '" & model & "' Where modelID = " & modelID & "<br><br>")
            Conn.Execute("Update tblModel Set model = '" & model & "' Where modelID = " & modelID & "")
            dbClose()
        Else
            dbOpen()
            Conn.Execute("Insert Into tblModel (brandID, model) Values(" & brandID & ",'" & model & "')")
            dbClose()
        End IF
        Response.Redirect("settingsModel.aspx?brandID=" & brandID & "&subDo=%20-%20SAVED")
    ElseIf Request.QueryString("do") = "delete" Then 
        brandID = Request.QueryString("brandID")
        modelID = Request.QueryString("modelID")
        brandID = CLng(brandID)
        modelID = CLng(modelID)
        dbVehicleCount = 1
        dbOpen()
        RecSet = Conn.Execute("Select Count(vehicleID) As vehicleCount From tblVehicle Where modelID = " & modelID & "")
        dbVehicleCount = RecSet.Fields("vehicleCount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If CInt(dbVehicleCount) = 0 And CDbl(fragusLeasingUserTypeID) = 1 Then 
            dbOpen()
            Conn.Execute("Delete From tblModel Where modelID = " & modelID & "")
            dbClose()
        End IF
        Response.Redirect("settingsModel.aspx?brandID=" & brandID & "&subDo=%20-%20DELETED")
    End IF
%>
